package com.infore.airsupervise.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.bean.QuestionE;
import com.infore.airsupervise.ui.fragment.ShowPhotoFragment;
import com.infore.airsupervise.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 问题详情
 */
public class QuestionDetailActivity extends BaseTitleBarActivity {
    @BindView(R.id.tv_question_title)
    TextView tvQuestionTitle;
    @BindView(R.id.tv_question_code)
    TextView tvQuestionCode;
    @BindView(R.id.tv_question_person)
    TextView tvQuestionPerson;
    @BindView(R.id.tv_question_releaseTime)
    TextView tvQuestionReleaseTime;
    @BindView(R.id.tv_question_content)
    TextView tvQuestionContent;
    @BindView(R.id.ll_photo_detail)
    LinearLayout llPhotoDetail;
    @BindView(R.id.tv_grid_area)
    TextView tvGridArea;
    @BindView(R.id.tv_grid_street)
    TextView tvGridStreet;
    @BindView(R.id.tv_grid_community)
    TextView tvGridCommunity;
    private ShowPhotoFragment showPhotoFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question_detail);
        ButterKnife.bind(this);
        Intent it = getIntent();
        if (it != null) {
            QuestionE questionE = (QuestionE) it.getSerializableExtra(Constants.INTENT_OBJ_KEY);
            initQuestionInfo(questionE);
            initPhotoDisplay(questionE.getPic());
        }
        initTitleBar();

    }

    private void initTitleBar() {
        title.setText("问题上报详情");
        rightMenu.setVisibility(View.INVISIBLE);
    }

    private void initQuestionInfo(QuestionE questionE) {
        try {
            tvQuestionTitle.setText(questionE.getQuestionTitle());
            tvQuestionCode.setText(questionE.getQuestionNum());
            tvQuestionReleaseTime.setText(questionE.getReleaseTime());
            tvQuestionContent.setText(questionE.getQuestionContent());
            tvQuestionPerson.setText(questionE.getUserName());
            //网格信息
            String [] grid = questionE.getGridName().split(",");
            tvGridCommunity.setText(grid[0]);
            tvGridStreet.setText(grid[1]);
            tvGridArea.setText(grid[2]);
        }catch (Exception ex){
            tvGridCommunity.setText("暂无");
            tvGridStreet.setText("暂无");
            tvGridArea.setText("暂无");
            ToastUtil.showShort(ctx,"网格信息解析错误");
        }
    }


    private List<String> photoPath = new ArrayList<>();//照片uri

    /**
     * 设置展示图片流的frament
     */
    private void initPhotoDisplay(List<String> pic) {
        if (null != pic && pic.size() > 0) {
            photoPath.addAll(pic);
            if (showPhotoFragment == null) {
                showPhotoFragment = new ShowPhotoFragment(photoPath, 200);
            }
            getSupportFragmentManager().beginTransaction().replace(R.id.fl_photo_show, showPhotoFragment)
                    .commit();
        } else {
            llPhotoDetail.setVisibility(View.GONE);
        }

    }
}
